<template>
  <div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <AjaxButton type="primary" :click="checkDialog" :disabled="disabledS">确 认</AjaxButton>
      <el-button @click="closeDialog">取 消</el-button>
    </div>

    <TopFilter :list="topFilterList" :cols="3" @filterChange="changeHandle" @onCollapse="collapseHandle"></TopFilter>

    <FilterTable
      ref="table"
      columnsRef="xmzOwnerModel"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      :isPagination="true"
      :onColumnsChange="columns => (this.columns = columns)"
      :isSelectColumn="true"
      :onSyncTableData="tableHandle"
      selectionType="single"
      :onRowSelectChange="selectChangeHandle"
      height="auto"
    >
      <template slot="controls" slot-scope="props"></template>
    </FilterTable>
  </div>
</template>
<script>
// @Author:徐铭泽

import { confirmAction, isFormEmpty } from '@/utils';
import { dictionary } from '@/utils/dictMixin';
import { getVins } from '@/api/customerRepair/customerInfo/ownerVehicleManagement';
import { getPinpSelect, getChexSelect, getCheModelSelect } from '@/api/repair/repairReception/customer/brand.js';
import Cookies from 'js-cookie';
import { notifyAction } from '../../../../../utils';

export default {
  name: '',
  props: {
    formData: {
      type: Object,
      default: {}
    }
  },
  mixins: [dictionary],
  data() {
    this.selectedRow = null;
    return {
      topFilterList: this.createTopFilters(),
      fetchApi: getVins,
      columns: this.createTableColumns(),
      fetchParams: { mobile: this.formData.mobile, vin: this.formData.vin, xhrAbort: true },
      tableData: [], // 表单数据
      brandList: [],
      disabledS: true
    };
  },
  mounted() {
    this.BaseTable = this.$refs.table;
    // 获取品牌
    this.getBrand();
  },
  methods: {
    selectChangeHandle(rows) {
      // if (!rows.length) {
      //   this.disabledS = false;
      //   this.selectedRow = null;
      // } else {
      //   this.disabledS = true;
      //   this.selectedRow = rows[0];
      // }
      this.disabledS = true;
      if (rows.length > 0) {
        this.disabledS = false;
        this.selectedRow = rows[0];
      }
    },
    createTopFilters() {
      return [
        {
          type: 'INPUT',
          label: 'VIN',
          fieldName: 'dph',
          initialValue: this.formData.vin,
          rules: [{ required: true, message: '请输入VIN', trigger: 'blur' }]
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '车牌号',
          dataIndex: 'license',
          showOverflowTooltip: true
        },
        {
          title: 'VIN',
          dataIndex: 'vin',
          width: 200,
          showOverflowTooltip: true
        },
        // {
        //   title: '车主姓名',
        //   dataIndex: 'ownerName',
        //   showOverflowTooltip: true
        // },
        {
          title: '品牌',
          dataIndex: 'brand',
          showOverflowTooltip: true,
          render: props => {
            return <span>{this.getText('brand', props.row.brand)}</span>;
          }
        },
        {
          title: '发动机号',
          dataIndex: 'engineNo',
          showOverflowTooltip: true
        },
        {
          title: '电机号1',
          dataIndex: 'motorNo1',
          showOverflowTooltip: true
        },
        {
          title: '电机号2',
          dataIndex: 'motorNo2',
          showOverflowTooltip: true
        },
        {
          title: '动力形式',
          dataIndex: 'powerType',
          dictItems: this.createDictList('6008'),
          filterItems:this.createDictList('6008')
        }
      ];
    },

    async checkDialog() {
      if (isFormEmpty(this.selectedRow)) {
        try {
          await confirmAction('请至少选择一条数据，再进行操作');
          return;
        } catch (err) {}
      } else {
        this.$emit('setCustomerInfo', this.selectedRow);
      }
    },
    // 搜索
    changeHandle(val) {
      // 查询销售接口传参vins为数组时才能精准查询
      // val.mobile = this.formData.mobile;
      val.appId = Cookies.get('appId');
      const vins = [];
      vins.push(val.dph);
      val.vinOne = vins;
      this.fetchParams = Object.assign({}, val);
    },
    closeDialog(param) {
      this.$emit('close', false, param);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    // table动态回调函数
    tableHandle(val) {
      this.tableData = val;
    },
    // 品牌
    async getBrand() {
      const res = await getPinpSelect();
      if (res.resultCode === 200) {
        if (res.errMsg) {
          notifyAction(res.errMsg, 'error');
        } else {
          this.brandList = res.data.map(x => ({
            value: x.brandId,
            text: x.brandName
          }));
        }
      }
    },
    // table列表翻译
    getText(type, val) {
      if (type === 'brand') {
        return this.brandList.find(x => x.value === val) ? this.brandList.find(x => x.value === val).text : '';
      }
    }
  }
};
</script>

<style lang="less" scoped></style>
